package com.anji.hyperneat.modular;

/**
 * Input mapper for arrays of float values; all values in array should share a common upper and lower bound, for proper normalization.
 * @author slusk
 *
 */
public class FloatArrayInputMapper implements ICppnInputMapper {

	protected int dims;
	protected float vals[];
	protected float minVal;
	protected float maxVal;
	
	/**
	 * Constructor.
	 * @param minVal Minimum possible value of any element in the array.
	 * @param maxVal Maximum possible value of any element in the array.
	 */
	public FloatArrayInputMapper(int dims, float minVal, float maxVal) {
		this.dims = dims;
		this.maxVal = maxVal;
		this.minVal = minVal;
	}
	
	@Override
	public float[] getInputs() {
		return vals;
	}

	@Override
	public int getNumInputs() {
		return dims;
	}

	/**
	 * Set the array for this mapper.
	 * @param vals
	 */
	public void populateValues(float[] vals) {
		if (vals.length != dims) throw new IllegalArgumentException("Input array size does not match size specified at construction time.");
		this.vals = vals;
	}

	@Override
	public float[] getNormalizedInputs() {
		float[] norms = new float[vals.length];
		for (int i = 0; i < norms.length; i++) {
			if (maxVal != minVal)
				norms[i] = ((vals[i] - minVal) / (maxVal - minVal)) * 2 - 1;
			else
				norms[i] = 0.0f;
		}
		return norms;
	}

}
